<?php
/**
 * 此类只适合小数据的分页，大数据分页不适用，大数据直接在database分好页读取
 * 使用方式：
 * 
	留言使用实例：
	Loader::loadApi('paginator');
	$p = isset($_GET['page'])?intval($_GET['page']):1;
	$pagesize = 10;
	$begin = ($p-1)*$pagesize;
	$mengx_count = $oo->get_mengx_count();
	$mengx = $oo->get_mengx('','order by posttime desc',"limit $begin,$pagesize");
	$total_page = ceil($mengx_count/$pagesize);
	$p =new Paginator($total_page,$mengx,$p,10,'http://act.51.com/2010/mxsj2/mengx?page={page}');

	########################

	$html['mengx'] = is_array($mengx) ? $mengx : array();
	$html['echopage'] = $p->repage();
	Loader::loadView('mxsj2/mengx', $html);


 */

class Paginator {
	public $cur_page=1;
	public $page_size=20;
	public $url='http://act.51.com/2010/mxsj2/mengx?page={page}';
	public $mid_area_num = 2;
	private static $instance;
	public $page_content;
	public $total_page;
	
	public static function getInstance($total_page, $content_arr, $cur_page=1, $page_size=20, $url='javascript:alert({page});', $mid_area_num = 2){
		if(!(self::$instance instanceof self)){
			self::$instance = new self($total_page, $content_arr, $cur_page, $page_size, $url,$mid_area_num);
		}
		return self::$instance;
	}
	
	function __construct($total_page, $content_arr, $cur_page=1, $page_size=20, $url='javascript:alert({page});', $mid_area_num = 2){
		echo '<link href="'.dirname(__FILE__).'/paginator.css" rel="stylesheet" />';
		if($total_page== 0) return false;
		$this->cur_page = intval($cur_page);
		$this->page_size = intval($page_size);
		$this->url = $url;
		$this->mid_area_num = intval($mid_area_num);
		$this->total_page = $total_page;
		if($this->cur_page > $this->total_page) return false;
		//给当前页面内容赋值
		$this->page_content = $content_arr;
		unset($content_arr);
	}
	
	public function repage(){
		//当只有一页时不显示本页的页码
		if($this->total_page > 1){
			$cur_href = '<a class="currentpage">{page}</a>';
		}else{
			$cur_href = '';
		}

		$base_html = $this->get_split_page($this->total_page,
								$this->cur_page,
								$this->page_size,
								'<a href="'.$this->url.'">{page}</a>',
								$cur_href,
								'<a href="'.$this->url.'">前一页</a>',
								'<a href="'.$this->url.'">后一页</a>',
								'<span>...</span>',
								$this->mid_area_num
								);
		
		//防止一个页面出现多个分页时冲突
		list($random_id, $sec) = explode(" ", microtime());
		$random_id = substr($random_id, 3);	
		if($this->total_page > 1){
			$gopage_html = '
						<span>跳到第</span>
							<form style="margin:0;" action="javascript:fo_gopage'.$random_id.'()">
								<input onpaste="return false" id="fo_go_'.$random_id.'" type="text" class="inputpage" />
							</form>
						<span>页</span>
						<input type="button" onclick="javascript:fo_gopage'.$random_id.'()" class="pagebtn" value="GO">';
		
		
			$script = '
			<script>
			function fo_gopage'.$random_id.'()
			{
				var num = document.getElementById(\'fo_go_'.$random_id.'\').value;
				if(!num)
					return;
				num = parseInt(num);
				if (isNaN(num)){
					alert("输入的页数无效");
					return ;
				}
				if( num < 1 )
					num = 1;
				if( num > '.$this->total_page.' )
					num = '.$this->total_page.';
				location.href= ("'. $this->split_page_tpl_replace($this->url, '{go_page}',$this->total_page). '").replace("{go_page}", num);
			}
			</script>
			';
		}else{//如果只有1页则不显示跳转 
			$gopage_html = '';
			$script = '';
		}
		return "<div class=\"g_page_center\"><div class=\"g_page\">$base_html $gopage_html $script </div></div>";
	}
	
	function split_page_tpl_replace($str, $page, $total_page){
		$tags = array('{page}', '{total_page}');
		$replaced_tag = array($page, $total_page);
		return str_ireplace($tags, $replaced_tag, $str);
	}
	
	function get_split_page($total_page,$cur_page = 1,
							$page_size = 20,
							$page_str = '<a href="javascript:alert(\'跳到第{page}\')">[{page}]</a>',
							$cur_page_str = '<b>{page}</b>',
							$pre_page_str = '<a href="javascript:alert(\'跳到第{page}\')">前一页</a>',
							$next_page_str = '<a href="javascript:alert(\'跳到第{page}\')">后一页</a>',
							$ellipsis_str = '...',
							$mid_area_num = 2){
		$cur_page = intval($cur_page);
		$page_size = intval($page_size);
		if($cur_page <= 0 ) $cur_page = 1;		
		if($cur_page > $total_page ) $cur_page = $total_page;
		//第一页，不需要"前一页"
		if($cur_page == 1){
			$pre_page_str = '';
			$first_page_str = '';
		}else{
			$first_page_str = $this->split_page_tpl_replace($page_str, 1, $total_page);
			$pre_page = $total_page==1?$total_page:$cur_page - 1;
			$pre_page_str = $this->split_page_tpl_replace($pre_page_str, $pre_page, $total_page);
		}
		//最后一页，不需要"后一页"
		if($cur_page == $total_page ){
			$next_page_str = '';
			$last_page_str = '';
		}else {
			$next_page = $cur_page + 1;
			$next_page_str = $this->split_page_tpl_replace($next_page_str, $next_page, $total_page);
			$last_page_str = $this->split_page_tpl_replace($page_str, $total_page, $total_page);
		}	
		$pre_str = '';
		//特殊需求，当省略号部分只有1个页码时，不显示... 直接显示页码
		if($cur_page - 1 == $mid_area_num + 2 )
			$pre_str .= $this->split_page_tpl_replace($page_str, 2, $total_page);
		else if ($cur_page - 1 > $mid_area_num + 2 )
			$pre_str .= $ellipsis_str;
		for($i = max($cur_page-$mid_area_num, 2); $i < $cur_page; $i++ ){
			$pre_str .= $this->split_page_tpl_replace($page_str, $i, $total_page);
		}	
		$next_str = '';
		for($i = $cur_page+1; $i < min($cur_page+$mid_area_num+1, $total_page); $i++){
			$next_str .= $this->split_page_tpl_replace($page_str, $i, $total_page);
		}
		//特殊需求，当省略号部分只有1个页码时，不显示... 直接显示页码
		if($total_page - $cur_page == $mid_area_num + 2 )
			$next_str .= $this->split_page_tpl_replace($page_str, $total_page-1, $total_page);
		else if ($total_page - $cur_page > $mid_area_num + 2 )
			$next_str .= $ellipsis_str;
	
		$cur_page_str = $this->split_page_tpl_replace($cur_page_str, $cur_page, $total_page);
		return "$pre_page_str $first_page_str $pre_str $cur_page_str $next_str $last_page_str $next_page_str";
	}
	
	function __destruct(){
		
	}
}

?>